package com.wangshouyu.mp.micro.qrcode;

import com.google.gson.Gson;
import com.wangshouyu.mp.micro.base.constant.UrlConstant;
import com.wangshouyu.mp.micro.base.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.OutputStream;
import java.text.MessageFormat;

/**
 * @author 王守钰
 * @description 小程序码工具
 * @date 2020/8/21 下午10:35
 */
public class QrCodeUtils {

    private QrCodeUtils(){}

    private static final Gson GSON = new Gson();

    private static Logger log = LoggerFactory.getLogger(QrCodeUtils.class);

    /**
     * 生成小程序码
     * @param accessToken 小程序accessToken
     * @param qrCode 小程序码
     * @param outputStream 输出流
     */
    public static void genQrCode(String accessToken, QrCode qrCode, OutputStream outputStream){
        String url = MessageFormat.format(UrlConstant.QR_CODE_UNLIMIT_URL, accessToken);
        try {
            HttpUtils.postDataStream(url,GSON.toJson(qrCode),null, outputStream);
        } catch (Exception e) {
            log.error("生成小程序码异常！");
            log.error(e.getMessage(), e);
        }
    }
}
